package com.team.order.controller;

import com.team.order.utils.PaymentUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.transaction.annotation.Transactional;

/**
 * 模拟支付回调控制器
 * 用于模拟第三方支付平台的回调
 * @author 梁国瑞
 */
@Slf4j
@RestController
@RequestMapping("/mock/callback")
@RequiredArgsConstructor
public class MockPaymentCallbackController {
    
    private final PaymentUtil paymentUtil;
    
    /**
     * 模拟支付成功回调
     */
    @PostMapping("/success/{orderNo}")
    @GlobalTransactional(rollbackFor = Exception.class)
    @Transactional(rollbackFor = Exception.class)
    public String paymentSuccessCallback(@PathVariable String orderNo) {
        try {
            log.info("模拟支付成功回调，订单号：{}", orderNo);
            
            // 直接调用PaymentUtil处理支付成功
            boolean success = paymentUtil.simulatePaymentSuccess(orderNo);
            
            log.info("支付成功回调处理完成，订单号：{}，结果：{}", orderNo, success);
            return success ? "SUCCESS" : "FAIL";
            
        } catch (Exception e) {
            log.error("支付成功回调处理异常，订单号：{}", orderNo, e);
            return "FAIL";
        }
    }
    
    /**
     * 模拟支付失败回调
     */
    @PostMapping("/fail/{orderNo}")
    @GlobalTransactional(rollbackFor = Exception.class)
    @Transactional(rollbackFor = Exception.class)
    public String paymentFailCallback(@PathVariable String orderNo) {
        try {
            log.info("模拟支付失败回调，订单号：{}", orderNo);
            
            // 直接调用PaymentUtil处理支付失败
            boolean success = paymentUtil.simulatePaymentFail(orderNo);
            
            log.info("支付失败回调处理完成，订单号：{}，结果：{}", orderNo, success);
            return success ? "SUCCESS" : "FAIL";
            
        } catch (Exception e) {
            log.error("支付失败回调处理异常，订单号：{}", orderNo, e);
            return "FAIL";
        }
    }
    
    /**
     * 模拟支付宝支付成功回调
     */
    @PostMapping("/alipay/success/{orderNo}")
    @GlobalTransactional(rollbackFor = Exception.class)
    @Transactional(rollbackFor = Exception.class)
    public String alipaySuccessCallback(@PathVariable String orderNo) {
        try {
            log.info("模拟支付宝支付成功回调，订单号：{}", orderNo);
            
            // 直接调用PaymentUtil处理支付宝支付成功
            boolean success = paymentUtil.simulatePaymentSuccess(orderNo);
            
            log.info("支付宝支付成功回调处理完成，订单号：{}，结果：{}", orderNo, success);
            return success ? "SUCCESS" : "FAIL";
            
        } catch (Exception e) {
            log.error("支付宝支付成功回调处理异常，订单号：{}", orderNo, e);
            return "FAIL";
        }
    }
} 